package com.cloudeye.im.controller;

import com.cloudeye.business.mq.config.RabbitMqImUserAddListener;
import com.cloudeye.business.mq.config.RabbitMqMsgTypeEnum;
import com.cloudeye.business.mq.model.MqMsg;
import com.cloudeye.business.mq.service.MqMsgSenderService;
import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.easemob.im.server.exception.EMBadRequestException;
import com.easemob.im.server.model.EMGroup;
import com.easemob.im.server.model.EMUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lafangyuan
 * @Date: 2021/11/23 14:27
 */
@Controller
@Slf4j
public class ImController {

    EMProperties properties = EMProperties.builder()
            .setAppkey("wnjk#chat")
            .setClientId("YXA6aip2eqBjQ9CZ34pQC37jTA")
            .setClientSecret("YXA6ID_D8QPZwK_1S8GERL00hx7CrIg")
            .setBaseUri("https://private-preview-a1.easemob.com")
            .build();

    EMService service = new EMService(properties);


    @Autowired
    MqMsgSenderService mqMsgSenderService;


    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/createAccount")
    @ResponseBody
    public String createAccount(@RequestParam("username")String username,@RequestParam("password")String password){

        try {
            service.user().create(username,password).block();
            return "saveSuccess";
        }catch (EMBadRequestException e){
            if(e.getMessage().contains("exists")){
                service.user().updateUserPassword(username,password);
                return "updateSuccess";
            }
            return e.getMessage();
        }


    }

    @RequestMapping("/mq/send")
    @ResponseBody
    public String sendMsg(@RequestParam("msg")String msg){
        MqMsg mqMsg = new MqMsg();
        mqMsg.setMsgId("1");
        mqMsg.setBody(msg);
        mqMsg.setSendTime(new Date());
        mqMsgSenderService.send(RabbitMqMsgTypeEnum.IM_USER_ADD,mqMsg);
        return "success";
    }


//    @RabbitListener(bindings = @QueueBinding(value = @Queue,
//            exchange = @Exchange(
//                    value ="#{com.cloudeye.business.mq.config.RabbitMqMsgTypeEnum.IM_USER_ADD.exchange}"),
//            key = "#{com.cloudeye.business.mq.config.RabbitMqMsgTypeEnum.IM_USER_ADD.routingKey}"
//    ))
    @RabbitMqImUserAddListener
    public void receiver(String data){
        log.info("接收到了消息..");
        log.info(data);
    }
    @RequestMapping("/userInfo")
    @ResponseBody
    public EMUser userInfo(@RequestParam("username")String username){
        EMUser user = service.user().get(username).block();
        return user;
    }
    @RequestMapping("/accountList")
    @ResponseBody
    public List<String> accoutList(){
        List<String> collect = service.user().listAllUsers().toStream()
                .collect(Collectors.toList());
        return collect;
    }

    @RequestMapping("/groupList")
    @ResponseBody
    public List<EMGroup> groupList(){
         return service.group().listAllGroups().toStream()
              .map(g->service.group().getGroup(g).block())
                 .collect(Collectors.toList());
    }

    @RequestMapping("/groupUsers")
    @ResponseBody
    public Map<String,List<String>> getGroupUsers(@RequestParam("groupId")String groupId){
        Map<String,List<String>> data = new HashMap<>();
        data.put("admins",service.group().listGroupAdmins(groupId).toStream().collect(Collectors.toList()));
        data.put("members",service.group().listGroupMembers(groupId,2000,null).block().getValues());
        return data;
    }


}
